package org.bbs.demo.reply.api;


import org.bbs.demo.advice.BusinessException;
import org.bbs.demo.common.BBSConstants;
import org.bbs.demo.common.Result;
import org.bbs.demo.reply.dao.ShowDao;
import org.bbs.demo.reply.dto.ReplyShowDto;
import org.bbs.demo.reply.dto.StoreyReplyDto;
import org.bbs.demo.reply.dto.StoreyReplyShowDto;
import org.bbs.demo.reply.dto.StringDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@RequestMapping("/comment")
@Transactional //事务声明，表示该类的所有方法都是事务性的，即方法中的所有访问数据库操作构成了一个整体事务，具有原子性
public class StoreyReplyShowLikeApi {
    @Autowired
    private ShowDao showdao;

    @PostMapping("/storeyReply/show/like")
    public Result info(@RequestBody StringDto stringdto){
        try{

            ArrayList<StoreyReplyShowDto> storeyReplys = new ArrayList<StoreyReplyShowDto>();
            storeyReplys.addAll(showdao.ShowStoreyReplyLike(stringdto.getR_id()));

            if(showdao.findRid(stringdto.getR_id()) == null){
                throw new BusinessException("评论不存在");
            }

            for(int i = 0;i < storeyReplys.size();i++){
                storeyReplys.get(i).setUser_name(showdao.FindUserName(storeyReplys.get(i).getUser_id()));
                storeyReplys.get(i).setBBS_user_name(showdao.FindUserName(storeyReplys.get(i).getBBS_user_id()));
                storeyReplys.get(i).setUser_head(showdao.FindUserHead(storeyReplys.get(i).getUser_id()));
                storeyReplys.get(i).setBBS_user_head(showdao.FindUserHead(storeyReplys.get(i).getBBS_user_id()));
            }
            return Result.OK("显示回复成功",storeyReplys);

        }catch (DuplicateKeyException err) {
            throw new BusinessException("显示失败");
            //return Result.err(BBSConstants.CODE_ERR_SYS, "显示失败");
        }
    }
}
